﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using EZCRS.HCMS.Entity;
using EZCRS.HCMS.Bll;
using EZCRS.HCMS.Bll.AIR;
using EZCRS.HCMS.Bll.baseinfo;
using EZCRS.HCMS.Web;

namespace EZCRS.HCMS.Web.manage.AIR
{
    public partial class hotel : ManageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (base.BasHotelId == 0)
                Response.Write(CFun.AlertMeg(Resources.Site.MustHotel, "back", ""));

            
            if (!IsPostBack)
            {
                CheckModule(69);

                CFun.SetListControl(ddlStatus, Bllbase_info.getbaseinfo(baseinfotype.房间状态, CFun.CurrentLanguage()), "");
                CFun.SetListControl(cblWeek, HotelBaseInfo.Weebly(), "");

                foreach (ListItem li in cblWeek.Items) li.Selected = true;

                startdate.Attributes.Add("onclick", "event.cancelBubble=true;showCalendarTA('startdate',false,'startdate','startdate',null);");
                startdate.Attributes.Add("onblur", "if(!chkdate(this.value))this.value='yyyy-MM-dd';");

                enddate.Attributes.Add("onclick", "event.cancelBubble=true;showCalendarTA('enddate',false,'enddate','enddate',null);");
                enddate.Attributes.Add("onblur", "if(!chkdate(this.value))this.value='yyyy-MM-dd';");
            }
        }

        /// <summary>
        /// 数据提交
        /// 根据日期范围进行按年拆分
        /// </summary>
        /// <param name="StartDate">起始日期</param>
        /// <param name="EndDate">结束日期</param>
        /// <param name="DayOfWeek">星期</param>
        /// <returns></returns>
        private bool SaveData(DateTime StartDate, DateTime EndDate, int[] DayOfWeek)
        {

            BllHCRS_AIR bll = new BllHCRS_AIR();

            //get data from page

            int iMin = 0, iMax = 0, iAdv = 0;

            int.TryParse(txtMin.Text, out iMin);
            int.TryParse(txtMax.Text, out iMax);
            int.TryParse(txtAdv.Text, out iAdv);

            int iYears = EndDate.Year - StartDate.Year;

            for (int i = 0; i <= iYears; i++)
            {

                #region 按年分隔日期

                DateTime dtStart = StartDate;
                DateTime dtEnd;

                if (iYears == 0)
                {
                    //同一年
                    dtEnd = EndDate;
                }
                else if (iYears == i)
                {
                    //最后一年

                    if (iYears >= 1)
                        dtStart = Convert.ToDateTime(dtStart.Year.ToString() + "-1-1");

                    dtEnd = EndDate;
                }
                else
                {
                    if (i > 0)
                        dtStart = Convert.ToDateTime(dtStart.Year.ToString() + "-1-1");

                    //设置成本年最后一天
                    dtEnd = Convert.ToDateTime(StartDate.AddYears(1).Year.ToString() + "-1-1").AddDays(-1);
                }

                #endregion

                HotelAIProvider ai = new HotelAIProvider();
                //HotelRateProvider hotelrate = new HotelRateProvider();


                //先从数据库读取对象

                HCRS_AIR air;

                string strWhere = "property_id =" + base.BasPropertyId + " AND level = 'P' AND Year = " + StartDate.Year.ToString();

                List<HCRS_AIR> airData = bll.getall(strWhere);

                if (airData.Count > 0)
                {
                    air = airData[0];

                    //为转换器加载数据
                    ai.Load(air);
                    //hotelrate.Load(air);

                }
                else
                    air = new HCRS_AIR();


                //将数据转换器内的数据更新
                ai.Create(dtStart, dtEnd, DayOfWeek, 0, ddlStatus.SelectedValue,
                    iMin, iMax, iAdv, "", "");

                //更新实体类对象
                air.PlanCode = "";
                air.RoomCode = "";
                air.Property_id = base.BasPropertyId;
                air.Year = ai.Year;
                air.Level = "P";

                air.Source = "DEFAULT";

                #region 数据转换

                byte[] bEmpty = new byte[1];

                //房型房量数据转换
                air.January_AI = ai.ConvertToBinaryData(1);
                air.February_AI = ai.ConvertToBinaryData(2);
                air.March_AI = ai.ConvertToBinaryData(3);
                air.April_AI = ai.ConvertToBinaryData(4);
                air.May_AI = ai.ConvertToBinaryData(5);
                air.June_AI = ai.ConvertToBinaryData(6);
                air.July_AI = ai.ConvertToBinaryData(7);
                air.August_AI = ai.ConvertToBinaryData(8);
                air.September_AI = ai.ConvertToBinaryData(9);
                air.October_AI = ai.ConvertToBinaryData(10);
                air.November_AI = ai.ConvertToBinaryData(11);
                air.December_AI = ai.ConvertToBinaryData(12);

                

                air.January_R = bEmpty;
                air.February_R = bEmpty;
                air.March_R = bEmpty;
                air.April_R = bEmpty;
                air.May_R = bEmpty;
                air.June_R = bEmpty;
                air.July_R = bEmpty;
                air.August_R = bEmpty;
                air.September_R = bEmpty;
                air.October_R = bEmpty;
                air.November_R = bEmpty;
                air.December_R = bEmpty;


                #endregion

                //写入数据库
                bool b = bll.insertorupdate(air);

                if (!b)
                    return false;


                StartDate = StartDate.AddYears(1);

            }

            return true;

        }


        protected void btnsave_Click(object sender, EventArgs e)
        {
            DateTime dtStart, dtEnd;

            if (!DateTime.TryParse(startdate.Value, out dtStart))
            {
                Response.Write(CFun.AlertMeg(Resources.Site.DateFormatError, "back", ""));
                return;
            }

            if (!DateTime.TryParse(enddate.Value, out dtEnd))
            {
                Response.Write(CFun.AlertMeg(Resources.Site.DateFormatError, "back", ""));
                return;
            }

            if (dtEnd < dtStart)
            {
                Response.Write(CFun.AlertMeg(Resources.Site.DateFormatError, "back", ""));
                return;
            }

            int[] iWeek = new int[7];

            foreach (ListItem item in cblWeek.Items)
            {
                if (item.Selected) iWeek[int.Parse(item.Value)] = 1; else iWeek[int.Parse(item.Value)] = 0;
            }

            bool b = SaveData(dtStart, dtEnd, iWeek);

            if (b)
                Response.Write(CFun.AlertMeg(Resources.Site.SystemAddSuccess, "back", ""));
            else
                Response.Write(CFun.AlertMeg(Resources.Site.SystemError, "back", ""));
        }


    }
}
